@echo off
setlocal enabledelayedexpansion

REM ====================================
REM  Docker Redeploy Script
REM  Project: UnitEchoose
REM ====================================

REM Set variables
set IMAGE_NAME=unitechoose
set CONTAINER_NAME=unitechoose
set TEMP_IMAGE_NAME=%IMAGE_NAME%-temp

REM Set data directory
set DATA_DIR=D:\docker\unitechoose\data

REM Set port mapping
set HOST_PORT=7605
set CONTAINER_PORT=8000

REM Set network
set NETWORK_NAME=mynet

echo ====================================
echo   Docker Redeploy Script
echo   Project: UnitEchoose
echo ====================================
echo.
echo [Configuration]
echo   Image Name: %IMAGE_NAME%
echo   Container Name: %CONTAINER_NAME%
echo   Port Mapping: %HOST_PORT%:%CONTAINER_PORT%
echo   Data Directory: %DATA_DIR%
echo   Network Name: %NETWORK_NAME%
echo.

REM Check if Docker is running
echo [Check] Checking if Docker is running...
docker info >nul 2>&1
if %errorlevel% neq 0 (
    echo [ERROR] Docker is not running or not installed!
    echo [TIP] Please start Docker Desktop first
    pause
    exit /b 1
)
echo [SUCCESS] Docker is running
echo.

REM Check if network exists
echo [Check] Checking if Docker network %NETWORK_NAME% exists...
docker network inspect %NETWORK_NAME% >nul 2>&1
if %errorlevel% neq 0 (
    echo [WARNING] Network %NETWORK_NAME% does not exist, creating...
    docker network create %NETWORK_NAME%
    if %errorlevel% neq 0 (
        echo [ERROR] Failed to create network!
        pause
        exit /b 1
    )
    echo [SUCCESS] Network created successfully
) else (
    echo [SUCCESS] Network already exists
)
echo.

REM Create data directory
echo [Check] Checking data directory...
if not exist "%DATA_DIR%" (
    echo [INFO] Data directory does not exist, creating...
    mkdir "%DATA_DIR%"
    if %errorlevel% neq 0 (
        echo [ERROR] Failed to create data directory!
        pause
        exit /b 1
    )
    echo [SUCCESS] Data directory created successfully
) else (
    echo [SUCCESS] Data directory already exists
)
echo.

REM Step 1: Build new temporary image
echo ====================================
echo   Step 1: Building temporary image
echo ====================================
echo [INFO] Building temporary image: %TEMP_IMAGE_NAME%
echo [TIP] This may take a few minutes, please wait...
echo.

docker build -t %TEMP_IMAGE_NAME% .
if %errorlevel% neq 0 (
    echo.
    echo [ERROR] Docker image build failed!
    echo [TIP] Please check Dockerfile and build logs
    pause
    exit /b 1
)

echo.
echo [SUCCESS] Temporary image built successfully!
echo.

REM Step 2: Stop and remove existing container
echo ====================================
echo   Step 2: Stop and remove existing container
echo ====================================
echo [INFO] Checking container: %CONTAINER_NAME%

REM Always try to stop and remove, ignore errors if container doesn't exist
echo [INFO] Attempting to stop container...
docker stop %CONTAINER_NAME% 2>nul
if %errorlevel% equ 0 (
    echo [SUCCESS] Container stopped
) else (
    echo [INFO] Container not running or does not exist
)

echo [INFO] Attempting to remove container...
docker rm %CONTAINER_NAME% 2>nul
if %errorlevel% equ 0 (
    echo [SUCCESS] Container removed
) else (
    echo [INFO] Container does not exist or already removed
)
echo.

REM Step 3: Remove old image
echo ====================================
echo   Step 3: Remove old image
echo ====================================
echo [INFO] Checking old image: %IMAGE_NAME%

REM Always try to remove, ignore errors if image doesn't exist
echo [INFO] Attempting to remove old image...
docker rmi %IMAGE_NAME% 2>nul
if %errorlevel% equ 0 (
    echo [SUCCESS] Old image removed
) else (
    echo [INFO] Old image does not exist or already removed
)
echo.

REM Step 4: Tag temporary image as final image
echo ====================================
echo   Step 4: Tagging image
echo ====================================
echo [INFO] Tagging temporary image as final image...

docker tag %TEMP_IMAGE_NAME% %IMAGE_NAME%
if %errorlevel% neq 0 (
    echo [ERROR] Image tagging failed!
    pause
    exit /b 1
)
echo [SUCCESS] Image tagged successfully

echo [INFO] Removing temporary image...
docker rmi %TEMP_IMAGE_NAME% >nul 2>&1
if %errorlevel% equ 0 (
    echo [SUCCESS] Temporary image removed
) else (
    echo [WARNING] Temporary image removal failed (does not affect deployment)
)
echo.

REM Step 5: Start new container
echo ====================================
echo   Step 5: Starting new container
echo ====================================
echo [INFO] Starting new container: %CONTAINER_NAME%
echo.

docker run -d ^
    --name %CONTAINER_NAME% ^
    -p %HOST_PORT%:%CONTAINER_PORT% ^
    -v %DATA_DIR%:/app/data ^
    --network=%NETWORK_NAME% ^
    --restart unless-stopped ^
    %IMAGE_NAME%

if %errorlevel% neq 0 (
    echo.
    echo [ERROR] Container start failed!
    echo [TIP] Please check if port is in use or view Docker logs
    pause
    exit /b 1
)

echo.
echo [SUCCESS] Container started successfully!
echo.

REM Wait for container to start
echo [INFO] Waiting for container to start (5 seconds)...
timeout /t 5 /nobreak >nul
echo.

REM Display container logs
echo ====================================
echo   Container Logs (Last 20 lines)
echo ====================================
docker logs --tail 20 %CONTAINER_NAME%
echo.

REM Deployment complete
echo ====================================
echo   Deployment Complete!
echo ====================================
echo.
echo [Access Information]
echo   Application URL: http://localhost:%HOST_PORT%
echo   Admin Panel: http://localhost:%HOST_PORT%/admin
echo.
echo [Common Commands]
echo   View logs: docker logs -f %CONTAINER_NAME%
echo   Stop container: docker stop %CONTAINER_NAME%
echo   Start container: docker start %CONTAINER_NAME%
echo   Restart container: docker restart %CONTAINER_NAME%
echo   Remove container: docker rm -f %CONTAINER_NAME%
echo.
echo [TIP] Press any key to open browser...
pause >nul

REM Open browser
start http://localhost:%HOST_PORT%

echo.
echo [SUCCESS] Browser opened!
echo.
pause

